﻿using System;
using System.Collections.Generic;
using System.Text;
using AIFSContentManagementSystem.Articles;
using AIFSContentManagementSystem.Criteria;

using AIFSContentManagementSystem.Specifications;

namespace AIFSContentManagementSystem.Criteria.Specifications
{
    /// <summary>
    /// Specification is used to test if a set of criteria is applicable to the object provided.
    /// </summary>
    public class MatchesCriteriaSpecification : ISpecification<ICriteriaSelectable>
    {
        List<Criterion> criteriaToMatch;

        public MatchesCriteriaSpecification(List<Criterion> CriteriaToMatch)
        {
            if (CriteriaToMatch == null || CriteriaToMatch.Count == 0)
                throw new ArgumentNullException("CriteriaToMatch can not be null or empty");

            criteriaToMatch = CriteriaToMatch;
        }

        public bool IsSatisfiedBy(ICriteriaSelectable CriteriaSelectable)
        {
            bool ismatch = false;

            //This object does not have any criteria so it is by default a match
            if (CriteriaSelectable == null || CriteriaSelectable.Criteria == null || CriteriaSelectable.Criteria.Count == 0)
                return true;

            foreach (Criterion criterion in CriteriaSelectable.Criteria)
            {
                ismatch = criterion.IsMatch(criteriaToMatch);

                if (ismatch)
                    break;
            }

            return ismatch;
        }
    }
}
